package parser;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.dom.Document;

import util.Candle;
import util.Util;

public class I3hsParser { 
	
	private LinkedHashMap<String, Integer> hotStocks;
	
	public int getHottness(String stockName) {
		if (hotStocks.get(stockName) == null) return -1;
		else return hotStocks.get(stockName);
	}
	
	public I3hsParser(String filename) {
		try {
			HashMap<String, Candle> candles = new HashMap<String, Candle>();
			Util.readFile(filename, candles);
			Set<String> sgxStockNames = candles.keySet();
			
	        String content = getText("http://sgx.i3investor.com/jsp/quotetrend.jsp"); 
	
	        String regex = "<tr> <td class=\"right\">(.*?)</td>.*?.jsp\">(.*?)</a>.*?.jsp\">(.*?)</a>.*?</tr>";
	        Pattern p = Pattern.compile(regex); 
	        Matcher m = p.matcher(content);
	
	        hotStocks = new LinkedHashMap<String, Integer>();
	        while (m.find()) {
	        	String content1 = m.group(0);
	        	int rank = Integer.parseInt(m.group(1).trim());
	        	String i3Name = m.group(2);
	        	String i3SgxName = m.group(3);
            	String matchedStockName = matchStockname(sgxStockNames, i3SgxName);
            	hotStocks.put(matchedStockName, rank);
            }
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
    private String matchStockname(Set<String> sgxStockNames, String stockName) {
    	String sgxStockName = "";
    	Iterator<String> iter = sgxStockNames.iterator();
    	while (iter.hasNext()) {
    		String iterStockName = iter.next();
    		if (iterStockName.contains(stockName.toUpperCase())) sgxStockName = iterStockName;
    	}

    	if (sgxStockName.equals("")) {
    		sgxStockName = "_" + stockName;
    	}
    	return sgxStockName;
    }
    
    private String getText(String url) throws Exception { 
        URL website = new URL(url); 
        URLConnection connection = website.openConnection(); 
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
 
        StringBuilder response = new StringBuilder(); 
        String inputLine; 
 
        while ((inputLine = in.readLine()) != null)  
            response.append(inputLine); 
 
        in.close(); 
 
        return response.toString(); 
    } 
 
    public static void main(String[] args) throws Exception { 
    	I3hsParser i3hsParser = new I3hsParser(args[0]);

		Iterator<String> iter = i3hsParser.hotStocks.keySet().iterator();
		while (iter.hasNext()) {
			String stockName = iter.next();
        	int rank = i3hsParser.hotStocks.get(stockName);
    		String paddedStockName = String.format("%1$-30s", stockName);
       		System.out.println(paddedStockName + "\t" + rank);
		}
    } 
} 
